﻿#include "CFDPostClipSliceWidgetBox.h"
#include "ui_CFDPostClipSliceWidgetBox.h"

#include "GUIFrame/MainWindow.h"
#include "PostGraphAdaptor/PostGraphWidgetBox.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostClipBox.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostSliceBox.h"

namespace GUI {
    CFDPostClipSliceWidgetBox::CFDPostClipSliceWidgetBox(ClipSlice widgetType, int currentDataID, int parentDataID, QWidget* parent) :
        CFDPostClipSliceWidgetBase(widgetType, parent), _parentDataID(parentDataID) {
        _currentDataID = currentDataID;

        _ui = new Ui::CFDPostClipSliceWidgetBox();
        _ui->setupUi(this);

        MainWindow* mainWin = FITKAPP->getGlobalData()->getMainWindowT<MainWindow>();
        if (mainWin == nullptr) {
            return;
        }
        QWidget* currentWidget = (QWidget*)this->getGraphWidget(); //mainWin->getRenderWidget()->getCurrentWidget();
        _boxWidget = new Interface::PostGraphWidgetBox(currentWidget);

        connect(_boxWidget, SIGNAL(sigValueChange(double*)), this, SLOT(slotVTKWidgetValueChange(double*)));
        connect(_ui->lineEdit_Point1X, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Point1Y, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Point1Z, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Point2X, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Point2Y, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Point2Z, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
    }

    CFDPostClipSliceWidgetBox::~CFDPostClipSliceWidgetBox() {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }

        if (_boxWidget) {
            delete _boxWidget;
            _boxWidget = nullptr;
        }
    }

    void CFDPostClipSliceWidgetBox::getDataFromWidget(Interface::FITKAbstractCFDPostData * postData) {
        double bounds[6] = { 0, 0, 0, 0, 0, 0 };
        bounds[0] = _ui->lineEdit_Point1X->value();
        bounds[1] = _ui->lineEdit_Point2X->value();
        bounds[2] = _ui->lineEdit_Point1Y->value();
        bounds[3] = _ui->lineEdit_Point2Y->value();
        bounds[4] = _ui->lineEdit_Point1Z->value();
        bounds[5] = _ui->lineEdit_Point2Z->value();

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                break;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipBox* currentData = dynamic_cast<Interface::FITKCFDPostClipBox*>(postData);
                if (currentData == nullptr) {
                    return;
                }
                currentData->setValue(bounds);
                break;
            }
            case GUI::ClipSlice::Slice: {
                Interface::FITKCFDPostSliceBox* currentData = dynamic_cast<Interface::FITKCFDPostSliceBox*>(postData);
                if (currentData == nullptr) {
                    return;
                }
                currentData->setValue(bounds);
                break;
            }
        }
    }

    void CFDPostClipSliceWidgetBox::setDataToWidget(Interface::FITKAbstractCFDPostData* postParentData, Interface::FITKAbstractCFDPostData* postData) {
        double bounds[6] = { 0, 0, 0, 0, 0, 0 };

        if (postParentData) {
            postParentData->getBounds(bounds);
        }

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                return;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipBox* currentData = dynamic_cast<Interface::FITKCFDPostClipBox*>(postData);
                if (currentData) {
                    currentData->getValue(bounds);
                }
                break;
            }
            case GUI::ClipSlice::Slice: {
                Interface::FITKCFDPostSliceBox* currentData = dynamic_cast<Interface::FITKCFDPostSliceBox*>(postData);
                if (currentData) {
                    currentData->getValue(bounds);
                }
                break;
            }
        }

        _ui->lineEdit_Point1X->setText(QString::number(bounds[0]));
        _ui->lineEdit_Point2X->setText(QString::number(bounds[1]));
        _ui->lineEdit_Point1Y->setText(QString::number(bounds[2]));
        _ui->lineEdit_Point2Y->setText(QString::number(bounds[3]));
        _ui->lineEdit_Point1Z->setText(QString::number(bounds[4]));
        _ui->lineEdit_Point2Z->setText(QString::number(bounds[5]));

        _boxWidget->setValue(bounds);
    }

    void CFDPostClipSliceWidgetBox::showEvent(QShowEvent * event) {
        if (_boxWidget) {
            _boxWidget->setIsShow(true);
            reRender();
        }
        CFDPostClipSliceWidgetBase::showEvent(event);
    }

    void CFDPostClipSliceWidgetBox::closeEvent(QCloseEvent * event) {
        if (_boxWidget) {
            _boxWidget->setIsShow(false);
            delete _boxWidget;
            _boxWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::closeEvent(event);
    }

    void CFDPostClipSliceWidgetBox::hideEvent(QHideEvent * event) {
        if (_boxWidget) {
            _boxWidget->setIsShow(false);
            delete _boxWidget;
            _boxWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::hideEvent(event);
    }

    void CFDPostClipSliceWidgetBox::slotVTKWidgetValueChange(double* bounds) {
        if (_ui == nullptr) {
            return;
        }
        _ui->lineEdit_Point1X->setText(QString::number(bounds[0]));
        _ui->lineEdit_Point2X->setText(QString::number(bounds[1]));
        _ui->lineEdit_Point1Y->setText(QString::number(bounds[2]));
        _ui->lineEdit_Point2Y->setText(QString::number(bounds[3]));
        _ui->lineEdit_Point1Z->setText(QString::number(bounds[4]));
        _ui->lineEdit_Point2Z->setText(QString::number(bounds[5]));
    }

    void CFDPostClipSliceWidgetBox::slotDataChange() {
        if (_boxWidget == nullptr) {
            return;
        }
        double bounds[6] = { 0, 0, 0, 0, 0, 0 };
        bounds[0] = _ui->lineEdit_Point1X->value();
        bounds[1] = _ui->lineEdit_Point2X->value();
        bounds[2] = _ui->lineEdit_Point1Y->value();
        bounds[3] = _ui->lineEdit_Point2Y->value();
        bounds[4] = _ui->lineEdit_Point1Z->value();
        bounds[5] = _ui->lineEdit_Point2Z->value();

        _boxWidget->setValue(bounds);
        reRender();
    }
}

